package com.sx.pipeline.ui.login

import android.content.Intent
import android.view.View
import android.webkit.JavascriptInterface
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ImmersionBar
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActLoginPageBinding
import com.sx.pipeline.qqlogin.BaseUIListener
import com.sx.pipeline.ui.login.adapter.LoginPageAdapter
import com.sx.pipeline.wxapi.WxUtils
import com.tencent.connect.common.Constants
import com.tencent.tauth.Tencent

/**
 * @Author: YY
 * @Date: 2022/4/6
 * @Description: 登录页面
 */
@Route(path = RoutePath.Login.LOGIN_PATH)
class LoginPageActivity : CYDataBindingBaseActivity<ActLoginPageBinding, LoginPageVM>
    (R.layout.act_login_page), View.OnClickListener {

    private lateinit var mTencent: Tencent
    private lateinit var mIUiListener: BaseUIListener

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)

        ImmersionBar.with(this)
            .fitsSystemWindows(false).init()

        // 初始化Pager
        initPager()
    }

    private fun initPager() {
        // viewPager的适配器
        mBinding.viewPager.adapter = LoginPageAdapter(
            mViewModel.getTabsList(),
            supportFragmentManager, lifecycle
        )
        //绑定tabLayout和viewPager
        TabLayoutMediator(
            mBinding.tab, mBinding.viewPager, true, true
        ) { tab, position ->
            tab.text = mViewModel.getTabsList()[position]
        }.attach()
    }

    fun isCheck(): Boolean{
        var checkAgreement= mBinding.toggleAgree.isChecked
        if(!checkAgreement){
            ToastUtils.showShort("请阅读并同意相关政策协议")
        }
        return checkAgreement
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.image_wechat -> {
                // 微信登录
                if(isCheck()){
                    val wxUtils = WxUtils()
                    wxUtils.auth()
                }

            }
            R.id.image_qq -> {
                if(isCheck()){
                    qqLogin()
                }
            }
            R.id.tv_privacy_agree -> {
                // 隐私协议
                mViewModel.getAgreement(1)
            }
            R.id.tv_user_agree -> {
                // 用户协议
                mViewModel.getAgreement(2)
            }
        }
    }

    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //腾讯QQ回调
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener)
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_LOGIN) {
                Tencent.handleResultData(data, mIUiListener)
            }
        }
    }

    /**
     * QQ登录
     */
    @JavascriptInterface
    fun qqLogin() {
        //传入参数APPID和全局Context上下文
        if (!this::mTencent.isInitialized) {
            mTencent = Tencent.createInstance(getString(R.string.qq_app_id), this)
        }
        mIUiListener = BaseUIListener(mTencent)
        //all表示获取所有权限
        mTencent.login(this, "all", mIUiListener)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 协议
        observe(mViewModel.agreementEvent) {
            RouteManager.Register.intentToAgreement(it!!)
        }
    }
}